package com.neuedu.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.his.mapper.CheckapplyMapper;
import com.neuedu.his.mapper.RegisterMapper;
import com.neuedu.his.po.Checkapply;
import com.neuedu.his.po.Register;
import com.neuedu.his.service.CheckapplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author root
 * @description 针对表【checkapply】的数据库操作Service实现
 * @createDate 2025-06-25 10:55:53
 */
@Service
public class CheckapplyServiceImpl extends ServiceImpl<CheckapplyMapper, Checkapply>
        implements CheckapplyService {

    @Autowired
    CheckapplyMapper checkmapper;

    @Autowired
    RegisterMapper regmapper;


    @Override
    public Map<String, Object> getinfoAndPriceByMedicalID(Integer mid) {
        // 1.查询患者信息
        QueryWrapper<Register> qw = new QueryWrapper<>();
        qw.eq("casenumber", String.valueOf(mid));
        Register reg = regmapper.selectOne(qw);
        // 2.查询患者的检验检查信息
        List<Map<String, Object>> list = checkmapper.getinfoByMedicalID(mid);

        // 返回结果
        Map<String, Object> map = new HashMap<>();
        System.out.println("传入病历号 mid = " + mid);

        System.out.println("查询患者信息 = " + reg);

        System.out.println("查询消费信息 list = " + list);

        map.put("info", reg);
        map.put("list", list);
        return map;
    }


    @Override
    public List<Checkapply> getCheckApplyByMedicalID(String medicalID) {
        QueryWrapper<Checkapply> wrapper = new QueryWrapper<>();
        // 这里medicalID是字符串，表里是int，需转换
        try {
            Integer mid = Integer.parseInt(medicalID);
            wrapper.eq("MedicalID", mid)
                    .in("State", 2, 3)  // 已收费(2)和已执行(3)的项目
                    .eq("RecordType", 2); // 2代表检验
            return list(wrapper);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    @Transactional
    public boolean chargeByIds(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) return false;

        // 批量更新检查项状态为“已收费”状态码2
        int updatedRows = checkmapper.updateStateByIds(ids, 2);
        return updatedRows == ids.size();
    }


    @Override
    @Transactional
    public boolean batchExecute(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) return false;

        // 将所有指定ID的记录状态更新为3（已执行）
        int updatedRows = checkmapper.updateStateByIds(ids, 3);
        return updatedRows == ids.size();
    }

    @Override
    @Transactional
    public boolean batchCancelExecute(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) return false;

        // 将所有指定ID的记录状态从3（已执行）恢复为2（已收费）
        int updatedRows = checkmapper.updateStateByIds(ids, 2);
        return updatedRows == ids.size();
    }

    @Override
    @Transactional
    public boolean inputResult(Integer id, String result, Integer resultOperID) {
        if (id == null || result == null || result.trim().isEmpty() || resultOperID == null) return false;

        // 更新指定检查项的结果内容、结果录入人、录入时间
        int updated = checkmapper.updateResult(id, result.trim(), resultOperID, LocalDateTime.now());
        return updated > 0;
    }



}